<template>
  <div class="container">

    <a-button class="btn" @click="uploadFile">点击上传</a-button>
    <a href="https://cdn2.hquesoft.com/box/import/%E5%BC%B9%E5%B9%95%E5%AF%BC%E5%85%A5%E7%A4%BA%E4%BE%8B.xlsx">
      查看导入Excel格式
    </a>

    <div v-if="info.uuid" style="margin-top: 30px;">
      总弹幕数:  {{info.total}}行
      <a-button class="btn" type="primary" @click="submitImport" style="margin-left: 30px;">确定导入此{{info.total}}行</a-button>
    </div>

    <!-- <IForm :formFrame="frame" class="form-container" :formData.sync="form"></IForm> -->
    <div slot="action" style="display: flex; justify-content: flex-end;">
      <a-button class="btn" @click="cancel">取消</a-button>
      <!-- <a-button class="btn" type="primary" @click="submit">提交</a-button> -->
    </div>
    <!-- <AttrEditor :form.sync="form">
			<div slot="action">
				<a-button class="btn" @click="cancel">取消</a-button>
				<a-button class="btn" type="primary" @click="submit">提交</a-button>
			</div>
		</AttrEditor> -->

  </div>
</template>

<script>
  // import AttrEditor from "./components/AttrEditor"
  // import IForm from "@/components/IForm"
  // import IUpload from "@/components/IUpload"

  const resourceType = 'danmus'

  export default {
    components: {
      // IForm,
      // IUpload
      // AttrEditor
    },
    data() {
      return {
        form: {},
        // type: 'lottery_egg',
        categoryList: [],
        info: {}
      }
    },
    props: {
      uuid: {
        type: String
      },
    },
    computed: {
    },
    created() {},
    mounted() {
      // this.uuid = this.uuid || this.$route.params.uuid

      // this.$http.get('/article/categories').then(res => {
      // 	this.categoryList = res.data.list
      // })

      // this.initForm()
    },
    watch: {
      uuid() {
        // this.initForm()
      }
    },
    methods: {
      submitImport() {
        this.$http.post(`/danmu/import/confirm`, {
          uuid: this.info.uuid
        }).then(res => {
          this.$message.success('导入成功')
          this.$emit('success')
        })
      },
      uploadFile() {
        this.$iUpload.show({
          title: '导入Excel格式线下样管文件',
          accept: '.xls,.xlsx',
          path: '/danmu/import/preview',
          success: (data) => {
            this.$message.success('文件上传成功，请在下方确认导入')
            // this.refreshData()
            this.info = data
            this.$iUpload.hide()
          }
        })
      },
      cancel() {
        this.uuid = ''
        this.$emit('cancel')
        // this.$router.back(-1)
      },
      create() {
        this.$http.post(`/${resourceType}`, {
          type: this.type,
          ...this.form
        }).then(res => {
          this.$message.success('创建成功')
          this.$emit('success')
          // this.$router.push({
          // 	path: './list'
          // })
        })
      },
      update() {
        this.$http.patch(`/${resourceType}/${this.uuid}`, {
          type: 'update',
          attributes: this.form
        }).then(res => {
          this.$message.success('更新成功')
          this.$emit('success')
          // this.uuid = ''
          // this.form = {}

          // this.$router.push({
          // 	path: '../list'
          // })
        })
      },
      submit() {
        if (this.uuid)
          this.update()
        else
          this.create()
      }
    }
  }
</script>

<style lang="scss" scoped>
  .form-container {
    padding-bottom: 120px;
  }

  .container {
    .btn {
      margin-right: 30px;
    }
  }
</style>
